﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Control;
using LPV_POS.Module;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos
{
    public partial class mh_dmthueVAT : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                label3.Text = rm.GetString("kieu", culture);
                label1.Text = rm.GetString("ten", culture);
                label4.Text = rm.GetString("giatri", culture);

                bton_tim.Text = rm.GetString("tim", culture);
                bton_clr.Text = rm.GetString("clr", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_Chon.Text = rm.GetString("chon", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                this.grid_thueVAT.GroupPanelText = rm.GetString("danhsachthue", culture); ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        // select danh sach don vi tinh
        private DataTable f_selectdmVAT()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmthueVAT", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "GIATRI";
            return dv_temp.ToTable();
        }

        // select danh sach don vi tinh cho combobox
        private DataTable f_selectdmVATforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmthueVAT", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["KIEU"] = "--- Chọn ĐVT ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // insert mot thue VAT
        private int f_insertthueVAT(string tenthueVAT, decimal giatri, string kieu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@GIATRI", "@KIEU" };
            o = new object[] { 2, 1, tenthueVAT, giatri, kieu };
            int ketqua = tv.SP_Update("sp_mh_dmthueVAT", s, o);
            return ketqua;
        }

        // update thue VAT
        private int f_updatethueVAT(string mathueVAT,string tenthueVAT, decimal giatri, string kieu)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@GIATRI", "@KIEU" };
            o = new object[] { 3, 1, mathueVAT, tenthueVAT, giatri, kieu };
            int ketqua = tv.SP_Update("sp_mh_dmthueVAT", s, o);
            return ketqua;
        }

        // xoa thue VAT
        private int f_deletethueVAT(string mathueVAT)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, mathueVAT };
            int ketqua = tv.SP_Update("sp_mh_dmthueVAT", s, o);
            return ketqua;
        }

        // Tim VAT khi nhan nut tim
        private DataTable f_selectfindvat(string kieuvat, string tenvat, decimal giatrivat)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@KIEU", "@OBJNAME", "@GIATRI" };
            o = new object[] { 1, 2, kieuvat, tenvat, giatrivat };
            tb = tv.SP_SelectDataTable("sp_mh_dmthueVAT", s, o);
            return tb;
        }
        // tim thue VAT
        private DataTable f_findthueVAT()
        {
            DataTable tb;
            string kieuvat;
            string tenvat;
            decimal giatrivat;
            if (com_kieuthueVAT.Text != "--- Chọn thuế VAT ---")
                kieuvat =  com_kieuthueVAT.Text ;
            else
                kieuvat ="";

            if (txt_tenthueVAT.Text != "")
                tenvat = "'%" + txt_tenthueVAT.Text + "%'";
            else
                tenvat = txt_tenthueVAT.Text;

            if (txt_giatrithueVAT.Text != "")
                giatrivat = Convert.ToDecimal( txt_giatrithueVAT.Text) ;
            else
                giatrivat = 0;

            tb = f_selectfindvat(kieuvat, tenvat, giatrivat);
            return tb;

        }

        // hien thi len combobox
        private void f_showdataoncombobox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
             cb._Init();
        }

        public mh_dmthueVAT()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            //  f_showdataoncombobox(com_kieuthueVAT, f_selectdmVATforcom(), "KIEU", "KIEU");
        }
        public mh_dmthueVAT(string sTenTieuDe)
        {            
            InitializeComponent();

            this.grid_thueVAT.GroupPanelText = "Danh sách " + sTenTieuDe.Trim().ToUpper();
            this.Tag = "";
            this.Text = "Danh sách " + sTenTieuDe;
            bton_Chon.Visible = true;
            bton_luu.Visible = false;
            bton_sua.Visible = false;
            bton_them.Visible = false;
            bton_tim.Visible = false;
            bton_xoa.Visible = false;
            col_mathueVAT.Visible = false;
            bton_clr.Visible = false;
        }

        private void mh_dmthueVAT_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            luoi_thueVAT.MainView = grid_thueVAT;
            luoi_thueVAT.DataSource = f_selectdmVAT();
         //   bton_tim.Text = "CLR";
            
            bton_luu.Enabled = false;
            bton_giatrithuVAT.Enabled = false;
            bton_tenthueVAT.Enabled = false;            
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

                this.Close();
            //}
        }

        private void bton_tim_Click(object sender, EventArgs e)
        {
            //if (bton_tim.Text == "CLR")
            //{
            //    com_kieuthueVAT.Text = "--- Chọn thuế VAT ---";
            //    txt_giatrithueVAT.Text = "";
            //    txt_tenthueVAT.Text = "";
            //    bton_tim.Text = "Tìm";
            //}
            //else
            //{
                luoi_thueVAT.DataSource = null;
                luoi_thueVAT.DataSource =  f_findthueVAT();
               
            //}
                
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_giatrithueVAT.Text = "";
            txt_tenthueVAT.Text = "";
            com_kieuthueVAT.SelectedIndex = 0;
            bton_giatrithuVAT.Enabled = true;
            bton_tenthueVAT.Enabled = true;
            bton_luu.Enabled = true;
            
            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_giatrithuVAT.Enabled = true;
            bton_tenthueVAT.Enabled = true;
            bton_luu.Enabled = true;

            Variable.str_trangthaithemsua = "sua";
        }

        private void grid_thueVAT_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_thueVAT.FocusedColumn;
            txt_giatrithueVAT.Text = grid_thueVAT.GetRowCellDisplayText(chiso, col_giatri) + "";
            txt_tenthueVAT.Text = grid_thueVAT.GetRowCellDisplayText(chiso, col_tenthueVAT) + "";
            string kieuthueVAT = grid_thueVAT.GetRowCellDisplayText(chiso, col_kieu) + "";
            //com_kieuthueVAT._SetselectedID("%");
      
            if (kieuthueVAT == "%")
                com_kieuthueVAT.SelectedIndex = 1;
            else if (kieuthueVAT == "d")
                com_kieuthueVAT.SelectedIndex = 2;
         
            bton_tenthueVAT.Enabled = false;
            bton_giatrithuVAT.Enabled = false;
            bton_luu.Enabled = false;
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_thueVAT.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string mathueVAT = grid_thueVAT.GetRowCellValue(grid_thueVAT.FocusedRowHandle, col_mathueVAT).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletethueVAT(mathueVAT);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_thueVAT.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_thueVAT.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
             
            if (txt_tenthueVAT.Text == "" || txt_giatrithueVAT.Text == "" || com_kieuthueVAT.Text == "--- Chọn thuế VAT ---")    // không thể luu neu chua nhap ten don vi tinh
                return;
                string kieu = com_kieuthueVAT.Text;
                string tenthueVAT="";
                string temp_tenthueVAT =txt_tenthueVAT.Text ;
                int chiso = grid_thueVAT.FocusedRowHandle;
                if (com_kieuthueVAT.Text == "%")
                {
                    if (temp_tenthueVAT.Substring(temp_tenthueVAT.Length - 1, 1) != "%") 
                        tenthueVAT = txt_tenthueVAT.Text + "%";
                    else
                        tenthueVAT = txt_tenthueVAT.Text;
                }
                else
                    tenthueVAT = txt_tenthueVAT.Text;

                decimal giatri = Convert.ToDecimal(txt_giatrithueVAT.Text);
                
                int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertthueVAT(tenthueVAT, giatri, kieu);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                string mathueVAT = grid_thueVAT.GetRowCellValue(grid_thueVAT.FocusedRowHandle, col_mathueVAT).ToString();
                ketqua = f_updatethueVAT(mathueVAT,tenthueVAT,giatri,kieu );

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // luoi_donvitinh.DataSource = f_selectdonvitinh();
            }
            luoi_thueVAT.DataSource = null;
            luoi_thueVAT.DataSource = f_selectdmVAT();
            if (Variable.str_trangthaithemsua == "sua")
                grid_thueVAT.FocusedRowHandle = chiso;
            else if (Variable.str_trangthaithemsua == "them")
                grid_thueVAT.FocusedRowHandle = grid_thueVAT.RowCount-1;

        }

        private void com_kieuthueVAT_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void bton_tenthueVAT_Click(object sender, EventArgs e)
        {
           
            string str_so = new cpublic().loadformso_point(670, 270);
            if (str_so == "")
                return;
            if (com_kieuthueVAT.Text == "%")
            {
                if (Variable.str_so.Substring(str_so.Length - 1, 1) != "%")
                {
                    txt_tenthueVAT.Text = str_so + "%";
                    txt_giatrithueVAT.Text = str_so;
                }

                else
                {
                    txt_tenthueVAT.Text = str_so;
                    txt_giatrithueVAT.Text = str_so;
                }

            }
            else
            {
                txt_tenthueVAT.Text = str_so;
                txt_giatrithueVAT.Text = str_so;
            }
           
        }

        private void bton_giatrithuVAT_Click(object sender, EventArgs e)
        {
            string _giatrithue = new cpublic().loadformso_point(670, 270);
            if (_giatrithue !="")
                txt_giatrithueVAT.Text = _giatrithue;
        }

        private void bton_Chon_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow dr1 = ((DataRowView)grid_thueVAT.GetRow(grid_thueVAT.FocusedRowHandle)).Row;
                this.Tag = dr1["OBJNAME"] + "";
                //new cpublic().Registry_Set("PHU_THU_PHANTRAM", this.Tag);
                this.Dispose();
            }
            catch (Exception EX)
            {
                string s = EX.Message;
            }
        }
        public string ShowForm()
        {
            this.ShowDialog();
            return this.Tag + "";
        }

        private void bton_clr_Click(object sender, EventArgs e)
        {
            com_kieuthueVAT.Text = "--- Chọn thuế VAT ---";
            txt_giatrithueVAT.Text = "";
            txt_tenthueVAT.Text = "";
            bton_tim.Text = "Tìm";
            bton_tenthueVAT.Enabled = true;
            bton_giatrithuVAT.Enabled = true;
        }

        private void txt_tenthueVAT_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_giatrithueVAT_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}